<template>
  <div class="box">
    <el-form
      :model="parkData"
      ref="form"
      label-width="80px"
      :inline="false"
      size="default"
      :props="{
        label: (data) => {
          // console.log('data', data);
        }
      }"
    >
      <el-form-item label="设备名称" prop="name">
        <el-input placeholder="请输入" v-model="parkData.name" />
      </el-form-item>
      <el-form-item label="设备人员" prop="relevance">
        <el-input placeholder="请输入" v-model="parkData.relevance" />
      </el-form-item>
      <el-form-item label="设备编号" prop="number">
        <el-input placeholder="请输入" v-model="parkData.number" />
      </el-form-item>
      <el-form-item label="设备简介" prop="intro">
        <el-input type="textarea" placeholder="请输入" v-model="parkData.intro" />
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="onSubmit" v-if="edata">确认修改</el-button>
        <el-button type="primary" @click="onSubmit" v-else>立即新增</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { parkPut, parkAdd } from '../../../api/Park/job.ts'
import { onMounted, ref } from 'vue'

const emit = defineEmits()
interface parkDataType {
  name: string
  number: string
  intro: string
  relevance: string
}
const parkData: parkDataType = reactive<parkDataType>({
  name: '',
  number: '',
  intro: '',
  relevance: ''
})

// 新增
const onSubmit = () => {
  if (props.edata) {
    console.log('表单信息', parkData)
    // 编辑
    let { objectId } = props.edata
    parkPut(objectId!, parkData).then(() => {
      emit('update:table-data', {
        ...parkData,
        objectId
      })
    })
  } else {
    parkAdd(parkData).then((res) => {
      emit('update:table-data', {
        ...parkData,
        objectId: res.data.objectId
      })
    })
  }
}

// 区分新增、编辑
// props数据具备单向性
type PropsType = {
  edata: parkDataType | null
}
const props = defineProps<PropsType>()
// console.log("接收到了父级的props", props.edata);
onMounted(() => {
  if (props.edata) {
    //点击到了编辑按钮
    let { name, number, intro, relevance } = props.edata
    parkData.name = name
    parkData.number = number
    parkData.intro = intro
    parkData.relevance = relevance
    // 修改
  } else {
    //点击到了新增按钮
    parkData.name = ''
    parkData.number = ''
    parkData.rank = ''
    parkData.intro = ''
    parkData.relevance = ''
  }
})
</script>

<style scoped>
.box :deep(.el-form-item__label) {
  box-sizing: content-box;
  width: 100px;
}
</style>
